var express = require('express');
var router = express.Router();
var request = require('request');

var CommonJS = require('../../common/common');
var DBConfig = require('../../db/dbconfig');
var mysql = require('mysql');
var pool = mysql.createPool(DBConfig.mysql);

// 复制原数据库中的订单
router.get('/jkyx/copy_orders', function(req, res, next) {
    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		connection.query(DBConfig.getOrders,function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
//				res.end(JSON.stringify({
//	     			msg: '数据库查询失败',
//	     			status:'102',
//	     			results: results
//	     		}));
//	     		return;
	     		var len = results.length;
	     		var values2 = '';
	     		for (var i = 0; i < len; i ++) {
	     			var account_id = results[i].account_id;
	     			var order_no = results[i].order_no;
	     			var consignee = results[i].consignee;
	     			var consignee_tel = results[i].mobile_number;
	     			var area_id = results[i].area_id;
	     			var address = results[i].address;
	     			var shooper_id = results[i].shooper_id;
	     			var defray_type = results[i].defray_id;
	     			var defray_id = results[i].delivery_point_id;
	     			var payment_id = results[i].payment_id;
	     			var invoice_type = results[i].invoice_type;
	     			var invoice_name = results[i].invo_cust_name;
	     			var freight_amount = results[i].freight_amount;
	     			var amount = results[i].amount;
	     			var pay_amount = results[i].pay_amount;
	     			var recharge_pay_amount = results[i].recharge_pay_amount;
	     			var gift_pay_amount = results[i].gift_pay_amount;
	     			var pay_amount_balance = results[i].pay_amount_balance;
	     			var pay_amount_other = results[i].pay_amount_other;
	     			var point_amount = results[i].point_amount;
	     			var point_use = results[i].point_use;
	     			var pay_timer = results[i].pay_time;
	     			var coupon_id = results[i].coupon_id;
	     			var order_type = '1';
	     			if (results[i].order_type == 1) {
	     				switch (results[i].source){
	     					case 3:
	     					order_type = '1';
	     						break;
	     					case 6:
	     					order_type = '3';
	     						break;
	     					default:
	     						break;
	     				}
	     			} else{
	     				order_type = '2';
	     			}
	     			var order_status = '4';
	     			switch (results[i].status){
	     				case 1:
	     				order_status = '6';
	     					break;
	     				case 2:
	     				order_status = '3';
	     					break;
	     				case 3:
	     				order_status = '4';
	     					break;
	     				default:
	     					break;
	     			}
	     			var shipping_status = results[i].shipping_status;
	     			var shipping_timer = results[i].shipping_time;
	     			var is_print_price = results[i].is_print_price;
	     			var remark = results[i].account_remark;
	     			var desc = results[i].desc;
	     			var status = results[i].is_account_del;
	     			var create_time = results[i].add_time;
	     			
	     			var values = "'" + account_id + "'" + ',' 
		  	  			 + "'" + order_no + "'" + ',' 
		  	  			 + "'" + consignee + "'" + ','
		  	  			 + "'" + consignee_tel + "'" + ','
		  	  			 + "'" + area_id + "'" + ','
		  	             + "'" + address + "'" + ',' 
		  	             + "'" + shooper_id + "'" + ',' 
		  	             + "'" + defray_type + "'" + ',' 
		  	             + "'" + defray_id + "'" + ',' 
		  	             + "'" + payment_id + "'" + ',' 
		  	             + "'" + invoice_type + "'" + ',' 
		  	             + "'" + invoice_name + "'" + ',' 
		  	             + "'" + freight_amount + "'" + ','
		  	             + "'" + amount + "'" + ','
		  	             + "'" + pay_amount + "'" + ','
		  	             + "'" + recharge_pay_amount + "'" + ','
		  	             + "'" + gift_pay_amount + "'" + ','
		  	             + "'" + pay_amount_balance + "'" + ','
		  	             + "'" + pay_amount_other + "'" + ','
		  	             + "'" + point_amount + "'" + ','
		  	             + "'" + point_use + "'" + ','
		  	             + "'" + pay_timer + "'" + ','
		  	             + "'" + coupon_id + "'" + ','
		  	             + "'" + order_type + "'" + ','
		  	             + "'" + order_status + "'" + ','
		  	             + "'" + shipping_status + "'" + ','
		  	             + "'" + shipping_timer + "'" + ','
		  	             + "'" + is_print_price + "'" + ','
		  	             + "'" + remark + "'" + ','
		  	             + "'" + desc + "'" + ','
		  	             + "'" + status + "'" + ','
		  	             + "'" + create_time + "'";
		  	             if (i == (len - 1)) {
			  	        		values = '(' + values + ')';
			  	        }else{
			  	        		values = '(' + values + '),';
			  	        }
			  	        values2 = values2 + values;
	     			
	     		}
				var inserSql = 'insert into orders values ' + values2 + ';';
				var pool2 = mysql.createPool(DBConfig.mysql2);
				pool2.getConnection(function(err, connection) {
					connection.query(inserSql,function (err, results) {
						if (err){
						  	res.end(JSON.stringify({
				     			msg: '数据库查询失败',
				     			status:'102',
				     			err: err
				     		}));
						}else{
							res.end(JSON.stringify({
				     			msg: '操作成功',
				     			status:'100',
				     		}));	      
						}
					})
				})   
			}
		})
	})
});


module.exports = router;
